<template>
	<kb-layout>
		
		<view class="cart-white u-padding-top-20">
			<view class="pdlr20">
				<up-search v-model="keyword" :clearabled="true" actionText="搜索" :showAction="true"
					height="30" searchIconSize="22" @custom="onKeyword" @search="onKeyword"
					:animation="true"></up-search>
			</view>
			
			<view class="search-tips pdlr20">
				<text>历史搜索</text>
				<view class="tags">
					<up-tag v-for="(tag,index) in historySearch" 
					:key="index"
					:text="tag" @click="()=>{keyword=tag; onKeyword()}" 
					plain shape="circle"></up-tag>
				</view>
				
				
			</view>
			
			<view class="search-tips pdlr20" v-if="keyword.length">
				<view>
					<text>在房源标题中搜索"</text> <text class="text-light">{{keyword}}</text> <text>"</text>
				</view>
				
			</view>
			
			<view v-if="result">
				<view v-for="(item,index) in result" :key="index">
					<view class="subtitle">
						<view class="pdlr20">
							<u-text size="12" :text="item.subtitle"></u-text>
						</view>				
					</view>
					<up-cell-group :border="false" >
						<up-cell  v-for="(house, k) in item.house" :key="k"
						 :isLink="true" 
						:url="'/pages/show/show?id='+house.id" :border="false"
						size="" row-direction="right">
						 <template #title>
							 <view class="u-line-1">
								  <text v-for="(text,vk) in house.titles" 
								  :key="vk"
								  :class="{'text-light':text.color}"> {{text.text}} </text>
							 </view> 
						 </template>
						 </up-cell>
						<up-cell  v-if="item.more==1"
						 :isLink="true"  title="查看更多房源"
						:url="'/pages/house/house?zhutype='+item.zhutype+'&keyword='+keyword" :border="false"
						size="" row-direction="right"></up-cell> 
					</up-cell-group>
					
				</view>
			</view>
			
			<up-empty v-if="isempty"
			 mode="data" text="暂无房源" 
			 :icon="ee.webUrl+'/images/empty/nodata.png'" > </up-empty>
			 
			
		</view>
		
		<kb-footer></kb-footer>
	</kb-layout>
</template>

<script>
import util from '../../common/tools/utils'
	export default {
		data() {
			return {
				keyword:'',
				result: undefined,
				isempty:undefined,
				history:undefined,
			}
		},
		computed:{
			historySearch(){
				if(this.history){
					let len = this.history.length;
					if( len > 10){
						 this.history =  this.history.slice(len-10, len-1 );						
					}
				}
				return this.history;
			}
		},
		methods: {
			onKeyword(word){				 
				this.search()
			},
			search(){
				if(this.keyword == '') return ;
				util.getModuleData('zufang/search', {
					'keyword': this.keyword
				}).then( ret => {
					if(!ret){
						this.isempty = true;
					}else{
						this.isempty = false;
						this.result = ret;
					 
						//搜索关键词记录 
						if(!this.history){
							this.history = [this.keyword]
						}else{
							if(!this.history.includes(this.keyword) ){								
								this.history.push(this.keyword)
							}							
						} 
						uni.setStorageSync('search-word', this.history)
					}					
					 
				})
			}
		},
		onLoad(option){
			if (option.keyword) {
				this.keyword = option.keyword
				this.search()
			}
			this.history = uni.getStorageSync('search-word');
			
		}
	}
</script>

<style lang="scss">
	.search-tips{
		margin-top: 30rpx;
		margin-bottom: 30rpx;
		font-size: 18px;
	}
	.text-light{
		color: #007AFF;
	}
	.subtitle{
		background-color: #F2F2F6;
		line-height: 60rpx;
		margin: 20rpx 0px;
	}
.cart-white{
	background:#fff;
 
	min-height: 1000rpx;
}
.pdlr20{
	margin-left: 30rpx;
	margin-right: 30rpx;
}
.tags{
	margin-top: 20rpx;
	display: flex;
	flex-wrap: wrap;
	justify-content: flex-start;
	gap:10rpx;
}
 
</style>
